<template>
  <div class="login_container">
    <div class="login_box">
      <div class="title">
        <h1>华麟管理系统</h1>
      </div>
      <!-- 表单 -->
      <el-form ref="LoginFormRef" :model="loginForm" label-width="0" :rules="LoginFormRules" class="login_form"
        @keyup.enter.native="login">
        <el-form-item prop="username">
          <!-- 用户名-->
          <el-input v-model="loginForm.username" placeholder="用户名" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <!-- 密码-->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="密码" prefix-icon="el-icon-lock" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { storage, pathConfig } from "@/config/configuration.js";
import * as homeApi from '@/api/web/homeApi';
import * as userApi from '@/api/admin/userApi';
import * as localStorage from '@/utils/localStorageUtil';

export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      LoginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      },
      logo: localStorage.getLocalStorage('logoUrl'),
    }
  },
  methods: {
    // 清空表单的校验
    resetLoginForm () {
      this.$refs.LoginFormRef.resetFields()
    },
    //登录的方法：登录逻辑写里面
    login () {
      //首先是校验如果正则的校验通过 -->> 执行数据传输
      this.$refs['LoginFormRef'].validate(async (valid) => {
        if (valid) {
          userApi.login(this.loginForm.username, this.loginForm.password)
            .then(res => {
              if (res.code == 200) {
                localStorage.setLocalStorage(storage.JWT, res.data, 1800000);
                this.$message({
                  message: '登录成功',
                  type: 'success'
                });
                this.$router.push(pathConfig.admin)
              }
              else if (res.code == 500) {
                this.$message({
                  message: res.exception,
                  type: 'error'
                })
              }
            });
        } else {
          return false
        }
      })
    },
    getLogoUrl () {
      homeApi.getLogoUrl()
        .then(res => {
          if (res.code == 200) {
            this.logo = res.data;
            localStorage.setLocalStorage('logoUrl', res.data, 1800000);
          }
        })
    },
  },
  mounted () {
    if (this.logo == null) {
      this.getLogoUrl();
    }
  }
}
</script>

<style scoped>
.login_container {
  background-color: #f3f3f3;
  height: 100%;
  border: 1px solid #DCDFE6;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%)
}

.avatar_box {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}

.avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.title {
  padding: 0px 20px;
  color: #303133;
}

.title h1 {
  font-weight: bold;
}
</style>